package com.dream.tihai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dream.tihai.entity.FileEntity;
import com.dream.tihai.mapper.FileMapper;
import com.dream.tihai.common.util.FileUtils;
import com.dream.tihai.common.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Valley
 * Created by Valley on 2022/12/13
 */
@RestController
@RequestMapping("file")
@CrossOrigin
public class FileController {

    @Value("${upload.dir}")
    String uploadDir;

    @Autowired
    FileMapper fileMapper;

    @RequestMapping("upload")
    public Map<String, Object> upload(MultipartFile file) throws Exception {
        Map<String, Object> map = new HashMap<>();
        if (file != null) {  //如果获取到的文件不为空
            String filename = file.getOriginalFilename();
            File file_server = new File(uploadDir, filename);  //创建文件对象
            if (!file_server.getParentFile().exists()) {
                //如果文件父目录不存在，就创建这样一个目录
                file_server.getParentFile().mkdirs();
            } else {  //如果父文件夹已经存在

            }
            file.transferTo(file_server);
            FileEntity entity = new FileEntity();
            entity.setFileName(filename);
            entity.setSaveDir(uploadDir);
            entity.setUploadTime(TimeUtils.nowToStr(System.currentTimeMillis()));
            fileMapper.insert(entity);
            map.put("status", true);
            map.put("msg", "上传文件成功");
        } else {   //如果获取到的文件为空
            map.put("status", false);
            map.put("msg", "上传文件失败");
            map.put("reason", "文件为空");
        }
        return map;
    }

    @RequestMapping("scan")
    public List<FileEntity> scan() throws Exception {
        LambdaQueryWrapper<FileEntity> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(FileEntity::getFlag, 0);
        List<FileEntity> fileEntities = fileMapper.selectList(wrapper);
        return fileEntities;
    }

    @RequestMapping("download")
    public void download(HttpServletRequest req, HttpServletResponse res, Integer id) throws Exception {
        FileEntity fileEntity = fileMapper.selectById(id);
        File download = new File(fileEntity.getSaveDir(), fileEntity.getFileName());
        FileUtils.downFile(req, res, fileEntity.getFileName(), download);
    }

    @RequestMapping("delete")
    public void delete(Long id) throws Exception {
        FileEntity fileEntity = fileMapper.selectById(id);
        File file = new File(fileEntity.getSaveDir(), fileEntity.getFileName());
        System.out.println("是否存在:"+file.exists());
        boolean delete = file.delete();
        System.out.println("删除:"+delete);
        int i = fileMapper.deleteById(id);
        System.out.println(i);
    }

}
